package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.PartDao;
import cn.b.sky.mes.em.model.Part;
import cn.b.sky.mes.em.model.condition.PartCondition;
import cn.b.sky.mes.em.service.PartService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("partService")
public class PartServiceImpl implements PartService {

  private static final Logger LOG = LoggerFactory.getLogger(PartServiceImpl.class);

  @Resource
  PartDao partDao;

  @Override
  public List<Part> selectPartListByCondition(PartCondition condition) {
    if (condition == null) {
      condition = new PartCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return partDao.selectPartListByCondition(condition);
  }

  @Override
  public int selectPartCountByCondition(PartCondition condition) {
    if (condition == null) {
      condition = new PartCondition();
    }
    return partDao.selectPartCountByCondition(condition);
  }

  @Override
  public Part getPart(String partCode, String siteCode) {
    if (StringUtils.isBlank(partCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return partDao.getPart(partCode, siteCode);
  }

  @Override
  public boolean updatePart(Part part) {
    if (part == null) {
      return false;
    }
    if (StringUtils.isBlank(part.getPartCode()) || StringUtils.isBlank(part.getSiteCode())) {
      return false;
    } else {
      partDao.updatePart(part);
      return true;
    }

  }

  @Override
  public boolean insertPart(Part part) {
    if (part == null) {
      return false;
    }
    if (StringUtils.isBlank(part.getPartCode()) || StringUtils.isBlank(part.getSiteCode())) {
      return false;
    } else {
      partDao.insertPart(part);
      return true;
    }

  }

  @Override
  public BatchJobReport deletePart(String partCode, String siteCode) {
    if (StringUtils.isBlank(partCode) || StringUtils.isBlank(siteCode)) {
      return new BatchJobReport("", LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
          SecurityContextUtil.getLanguageCode(), "parameterError.M"), false);
    } else {
      String message = partCode;
      String cause = "";
      boolean success = true;
      try {
        partDao.deletePart(partCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    }
  }

  @Override
  public List<Part> selectPartListForSuggest(PartCondition condition) {
    return partDao.selectPartListForSuggest(condition);
  }

}
